package jp.co.htkk.mapper;

import jp.co.htkk.dto.${TableName!}Dto;
import jp.co.htkk.entity.${TableName!};
import org.apache.ibatis.annotations.*;

@Mapper
public interface ${TableName!}Mapper {

    /**
     * @param ${tableName!}
     * @return number of ${tableName!} insert
     */
    @Options(useGeneratedKeys = true, keyProperty = "${keyName!}", keyColumn = "${key_name!}")
    @Insert("INSERT INTO ${table_name!}(<#list  columns as field> ${field.column_name!}<#if field_has_next>,</#if> </#list>) VALUES "
            + "(<#list  columns as field> ${r"#{"}${field.columnName!}${r"}"}<#if field_has_next>,</#if> </#list>)"
    )
    int create${TableName!}(${TableName!} ${tableName!});

    /**
     * @param id
     * @return ${TableName!}Dto
     */
    @Select("SELECT <#list  columns as field> ${field.column_name!}<#if field_has_next>,</#if> </#list> FROM ${table_name!} " +
            "WHERE ${key_name!} = ${r"#{id}"}")
    ${TableName!}Dto get${TableName!}ById(String id);

    @Delete("DELETE FROM ${table_name!} WHERE ${key_name!} = ${r"#{id}"}")
    int delete${TableName!}ById(String id);

    @Update("<script> " +
            "UPDATE ${table_name!}\n" +
            "        <trim prefix=\"SET\" suffixOverrides=\",\">\n" +
<#list  columns as field>
            "            <if test=\"${field.columnName!} != null\">${field.column_name!} = ${r"#{"}${field.columnName!}${r"}"},</if>\n" +
</#list>
            "        </trim>\n" +
            "        WHERE ${key_name!} =${r"#{id}"} " +
            "</script>")
    int update${TableName!}(${TableName!}Dto ${tableName!}Dto);
}
